<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model{
    function __construct(){
        parent::__construct();
    }

    public function validate(){
        // grab user input
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));

        // Prep the query
        $this->db->where('username', $username);
        $this->db->where('password', $password);

        // Run the query
        $query = $this->db->get('users');
        // Let's check if there are any results
        if($query->num_rows == 1)
        {
            // If there is a user, then create session data
            $row = $query->row();
            $data = array(
                'userid' => $row->userid,
                'name' => $row->name,
                'username' => $row->username,
                'email' => $row->email,
                'group' => $row->group,
                'validated' => true
            );
            $this->session->set_userdata($data);
            return true;
        }
        return false;
    }
    public function add_user()
    {
        $name = $this->security->xss_clean($this->input->post('name'))." ".$this->security->xss_clean($this->input->post('surname'));
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));
        $email = $this->security->xss_clean($this->input->post('email'));
        $data = array(
            'name'=>$name,
            'username'=>$username,
            'password'=>$password,
            'email'=>$email,
            'status'=>"user"
        );
        $this->db->insert('users',$data);
    }
    public function passwords_check()
    {
        $password = $this->security->xss_clean($this->input->post('password'));
        $password_repeat = $this->security->xss_clean($this->input->post('password_repeat'));

        if(strlen($password)<6 || strlen($password_repeat)<6)
        {
            return "tooSmallPassword";
        }
        if($password!=$password_repeat)
        {
            return "notMatchingPasswords";
        }

        else return "correctPassword";
    }
    public function user_exists()
    {
        $username = $this->security->xss_clean($this->input->post('username'));
        $this->db->where('username', $username);
        $query = $this->db->get('users');
        if($query->num_rows > 0)
        {
            return true;
        }
        return false;
    }
    public function fields_check()
    {
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));

        if(strlen($username)>0 && strlen($password)>0)
        {
            return true;
        }
        else return false;
    }
    public function fields_length_check()
    {
        $name = $this->security->xss_clean($this->input->post('name'))." ".$this->security->xss_clean($this->input->post('surname'));
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));
        $email = $this->security->xss_clean($this->input->post('email'));

        if(strlen($name) >64 || strlen($username)>64|| strlen($password)>64|| strlen($email)>64)
        {
            return false;
        }
        else return true;
    }
}
?>